<?php

namespace app\shop\controller;

// 框架基础控制类
use think\Controller;
use think\Request;

class Product extends Controller
{
    //构造函数
    public function __construct()
    {
        //手动把父类的方法获取过来
        parent::__construct();

        // check_cors_request();

        // 全局用户模型
        $this->BusinessModel = model('Business.Business');
        $this->RecordModel = model('Business.Record');
        $this->ProductModel = model('Product.Product');
        $this->CollectionModel = model('Business.Collection');
        $this->OrderProductModel = model('Product.OrderProduct');
    }

    // 列表
    public function list()
    {
        $keyword = $this->request->param('keyword', '', 'trim');
        $where = [];
        if (!empty($keyword)) {
            $where['name'] = ['like', "%$keyword%"];
        }
        $result =  $this->ProductModel->where($where)->order('id DESC')->paginate(10, false, ['query' => Request::instance()->param()]);

        if (count($result) > 0) {
            $this->success('', null, $result);
        } else {
            $this->error('', null, []);
        }
    }

    // 详情
    public function info()
    {
        if ($this->request->isPost()) {
            $proid = $this->request->param('proid', 0, 'trim');

            $busid = $this->request->param('busid', 0, 'trim');

            $info =  $this->ProductModel->find($proid);

            if (!$info) {
                $this->error('当前商品不存在');
            }

            // 判断产品详情是否有登陆，有的话就查询购物车数量
            if ($busid > 0) {
                $collect = $this->CollectionModel->where(['busid' => $busid, 'proid' => $proid])->find();
            }

            // 平均分
            $average =  $this->OrderProductModel->where(['proid' => (string)$proid])->avg('rate');

            $product['average'] = $average;

            // $where = [ 'proid'=> (string)$proid, 'comment'=> "not null"];

            // 评价详情
            $ratelist = $this->OrderProductModel->withJoin('user')->where('proid', (string)$proid)
                ->where('comment', 'not null')->select();

            $data = [
                'collect' => isset($collect) ? $collect : null,
                'info' => $info
            ];



            $this->success('', '', $data);
        }
    }

    // 收藏
    public function collect()
    {
        if ($this->request->isPost()) {
            $proid = $this->request->param('proid', 0, 'trim');

            $busid = $this->request->param('busid', 0, 'trim');

            $info =  $this->ProductModel->find($proid);

            if (!$info) {
                $this->error('当前商品不存在');
            }

            $userinfo =  $this->BusinessModel->find($busid);

            if (!$userinfo) {
                $this->error('当前用户不存在');
            }

            $collect = $this->CollectionModel->where(['busid' => $busid, 'proid' => $proid])->find();

            if ($collect) {
                $result = $this->CollectionModel->destroy($collect['id']);
                if ($result) {
                    $this->success('取消收藏成功');
                } else {
                    $this->error('取消收藏失败');
                }
            } else {
                $data = [
                    'busid' => $busid,
                    'proid' => $proid
                ];
                $result = $this->CollectionModel->save($data);
                if ($result) {
                    $this->success('收藏成功');
                } else {
                    $this->error('收藏失败');
                }
            }
        }
    }
}
